package com.central.apps.controller;

import com.central.app.service.HandlerService;
import lombok.extern.slf4j.Slf4j;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/Instorage")
public class ReceiptDoc {
    @Autowired
   private HandlerService handlerService;
    @PostMapping("/ReceiptDoc")

    public Object operated(@RequestParam String idt) throws Exception {

        //查询接收入库主表gq_instorage
        List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_instorage where id='"+idt+"'");
        Map<String, Object> map = bySql.get(0);
        String instonum = MapUtils.getString(map, "instonum");
        String instodesc = MapUtils.getString(map, "instodesc");
        String status = MapUtils.getString(map, "status");
        String types = MapUtils.getString(map, "type");
        String receiptnum = MapUtils.getString(map, "receiptnum");
        String createuserdesc = MapUtils.getString(map, "createuserdesc");
        String createtime = MapUtils.getString(map, "createtime");
        List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_instoline where instonum ='" + instonum + "'");
        if (CollectionUtils.isNotEmpty(bySql1)){
            for (int i=0;i<bySql1.size();i++){
                Map<String, Object> map1 = bySql1.get(i);
                String itemnum = MapUtils.getString(map1, "itemnum");//物料编码
                String itemdesc = MapUtils.getString(map1, "itemdesc");//物资描述
                Float innum = MapUtils.getFloat(map1, "innum");//数量
                String batchnum = MapUtils.getString(map1, "batchnum");//批次
                String binnum = MapUtils.getString(map1, "binnum");//货位
                String storageroom = MapUtils.getString(map1, "storageroom");//库房
                Float linecost = MapUtils.getFloat(map1, "linecost");//行成本
                Float unitcost = MapUtils.getFloat(map1, "unitcost");//单位成本
                String reqdeliverydate = MapUtils.getString(map1, "reqdeliverydate");//入库日期
                String life = MapUtils.getString(map1, "life");//失效日期
                String taskid = MapUtils.getString(map1, "taskid");//任务id
                String proid = MapUtils.getString(map1, "proid");//项目id
                String ponum = MapUtils.getString(map1, "ponum");//订单编号
                List<Map<String, Object>> bySql2 = handlerService.findBySql("select * from gq_item where itemnum ='" + itemnum + "'");
                Map<String, Object> map2 = bySql2.get(0);
                Float standardcost = MapUtils.getFloat(map2, "standardcost");
                String itemtype = MapUtils.getString(map2, "itemtype");
                String lifemoney = MapUtils.getString(map2, "lifemoney");
                String model = MapUtils.getString(map2, "model");
                String spemodel = MapUtils.getString(map2, "spemodel");
                String unit = MapUtils.getString(map2, "unit");//订购单位
                String outunit = MapUtils.getString(map2, "outunit");//发放单位
                String manufacturer = MapUtils.getString(map2, "manufacturer");//生产厂家
                String packform = MapUtils.getString(map2, "packform");//封装形式
                String maintechtors = MapUtils.getString(map2, "maintechtors");//封装形式
                String qualitygrade = MapUtils.getString(map2, "qualitygrade");//质量等级

                //库存成本表，
                List<Map<String, Object>> bySql3 = handlerService.findBySql("select avgcost from gq_invcost where itemnum='" + itemnum + "'and storenum='" + storageroom + "' and batchnum ='" + batchnum + "' and taskid ='" + taskid + "' and proid='" + proid + "'");
                if (CollectionUtils.isNotEmpty(bySql3)){
                    //查询对应的库存余量表，获取余量
                    List<Map<String, Object>> bySql4 = handlerService.findBySql("select * from gq_invbalances where itemnum='" + itemnum + "'and storenum='" + storageroom + "' and batchnum ='" + batchnum + "' and taskid ='" + taskid + "' and proid='" + proid + "' and binnum ='" + binnum + "'");
                    if (CollectionUtils.isNotEmpty(bySql4)){
                        float zong = 0;
                        float avg = 0;
                        float avgcost = 0;
                        for (Map<String,Object> mapl:bySql4){
                            Float curbal = MapUtils.getFloat(mapl, "curbal");
                            zong =zong + curbal;
                        }
                        Map<String, Object> map3 = bySql3.get(0);
                        Float avgcost1 = MapUtils.getFloat(map3, "avgcost");
                        avg =avgcost1;//库存平均价
                        if ("接收".equals(types)){
                            avgcost = (innum*unitcost + zong*avg)/(innum+zong); //总的平均价格
                        }else {
                            avgcost = (zong * avg - innum*unitcost)/(zong-innum);//总的平均价格
                        }
                        handlerService.updateBySql("update gq_invcost set avgcost= '" + avgcost + "',lastcost = '" + unitcost + "' where itemnum='" + itemnum + "'and storenum='" + storageroom + "' and batchnum ='" + batchnum + "' and taskid ='" + taskid + "' and proid='" + proid + "'");
                    }
                }else {
                    HashMap<String, Object> hashMap = new HashMap<>();
                    hashMap.put("batchnum",batchnum);
                    hashMap.put("taskid",taskid);
                    hashMap.put("proid",proid);
                    hashMap.put("avgcost",unitcost);
                    hashMap.put("itemnum",itemnum);
                    hashMap.put("storenum",storageroom);
                    hashMap.put("stdcost",standardcost);
                    handlerService.insert("gq_invcost",hashMap);
                }
                //更新库存
                Map<String, Object> mapTory = new HashMap<>();
                mapTory.put("itemnum",itemnum);//物料编码
                mapTory.put("itemdesc",itemdesc);////物料名称
                mapTory.put("storenum",storageroom);//仓库编号
                mapTory.put("curbaltotal",innum);//库存余量
                mapTory.put("avblbalance",innum);//可用余量
                mapTory.put("moneylife",lifemoney);//寿命
                mapTory.put("itemtype",itemtype);//物资类型
                mapTory.put("model",model);//规格型号
                mapTory.put("spemodel",spemodel);//品牌
                mapTory.put("manufacturer",manufacturer);//生产厂家
                mapTory.put("packagform",packform);//封装形式
                mapTory.put("orderunit",unit);//订购单位
                mapTory.put("outunit",outunit);//发放单位
                 mapTory.put("maintechindicat",maintechtors);//主要技术指标

                //插入库存余量
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("itemnum",itemnum);
                hashMap.put("receiptnum",receiptnum);
                hashMap.put("ponum",ponum);
                hashMap.put("storenum",storageroom);
                hashMap.put("binnum",binnum);
                hashMap.put("batchnum",batchnum);
                hashMap.put("taskid",taskid);
                hashMap.put("proid",proid);
                hashMap.put("avblbalance",innum);
                hashMap.put("curbal",innum);
                hashMap.put("unitcost",unitcost);
                hashMap.put("startdate",reqdeliverydate);
                hashMap.put("enddate",life);
                //查询库存主表，获取库存余量表 库 余量表这条数据查询，
                List<Map<String, Object>> bySql4 = handlerService.findBySql("select * from gq_invbalances where itemnum ='" + itemnum + "' and storenum ='" + storageroom + "' and binnum ='" + binnum + "' and batchnum ='" + batchnum + "' and taskid ='" + taskid + "' and proid ='" + proid + "'");
                if (CollectionUtils.isNotEmpty(bySql4)){
                    //更新库存余量表
                    Map<String, Object> map3 = bySql4.get(0);
                    Float curbal = MapUtils.getFloat(map3, "curbal");  //当前余量
                    Map<String, Object> map4 = bySql4.get(0);
                    Float avblbalance = MapUtils.getFloat(map4, "avblbalance");//可用余量
                    if ("接收".equals(types)){
                        curbal = curbal + innum;
                        avblbalance = avblbalance + innum;
                    }else {
                        curbal = curbal - innum;
                        avblbalance = avblbalance - innum;
                    }
                    handlerService.updateBySql("update gq_invbalances set curbal = '" + curbal + "',avblbalance = '" + avblbalance + "' where itemnum ='" + itemnum + "' and storenum ='" + storageroom + "' and binnum ='" + binnum + "' and batchnum ='" + batchnum + "' and taskid ='" + taskid + "' and proid ='" + proid + "'");
                }else {
                    //新增库存余量表信息
                    handlerService.insert("gq_invbalances", hashMap);
                }
                //查询库存主表信息
                List<Map<String, Object>> bySql5 = handlerService.findBySql("select * from gq_inventory where  itemnum  ='" + itemnum + "'and storenum ='" + storageroom + "'");
                if (CollectionUtils.isNotEmpty(bySql5)){
                    //库存余量表
                    List<Map<String, Object>> bySql6 = handlerService.findBySql("select * from gq_invbalances where  itemnum  ='" + itemnum + "'and storenum ='" + storageroom + "'");
                    float curbal = 0;//库存余量
                    float avblbalance = 0;//可用余量
                    if (CollectionUtils.isNotEmpty(bySql6)){
                        for (Map<String,Object> objectMap :bySql6){
                            Float curbal1 = MapUtils.getFloat(objectMap, "curbal");
                            curbal = curbal+curbal1;//库存余量
                            Float avblbalance1 = MapUtils.getFloat(objectMap, "avblbalance");

                            avblbalance = avblbalance+avblbalance1;//库存可用余量
                        }
                        handlerService.updateBySql("update gq_inventory set curbaltotal = " + curbal + ", avblbalance = " + avblbalance +  " where storenum = '" + storageroom + "' and itemnum = '" + itemnum + "'");
                    }
                }else {
                    handlerService.insert("gq_inventory",mapTory);
                }
                //接收收据写下入到库存交易表中
                HashMap<String, Object> hashMap1 = new HashMap<>();
                hashMap1.put("instonum",instonum);
                hashMap1.put("instodesc",instodesc);
                hashMap1.put("itemnum",itemnum);
                hashMap1.put("storenum",storageroom);
                hashMap1.put("itemdesc",itemdesc);
                hashMap1.put("receiptnum",receiptnum);
                hashMap1.put("quantity",innum);
                hashMap1.put("createuserdesc",createuserdesc);
                hashMap1.put("createtime",createtime);
                hashMap1.put("status",status);
                hashMap1.put("type","接收");
                handlerService.insert("gq_matusetrans",hashMap1);

            }
        }
       return "成功";
    }
}
